using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Xml.Serialization;
using WozLib.Xml;

namespace WozLib.UI.Ink.SketchCommands
{
  public class WindowResizeWozCommand : RememberSketchCommand
  {

    [XmlElement("NewSize")]
    public Size NewSize
    {
      get { return _NewSize; }
      set { _NewSize = new Size(value.Width,value.Height); }
    } private Size _NewSize;

    [XmlElement("PreviousSize")]
    public Size PreviousSize
    {
      get { return _PreviousSize; }
      set { _PreviousSize = new Size(value.Width, value.Height); }
    } private Size _PreviousSize;

    [XmlElement("RememberCommand")]
    public bool RememberCommand
    {
      get { return _RememberCommand; }
      set { _RememberCommand = value; }
    } private bool _RememberCommand;

    public WindowResizeWozCommand(Size newSize, Size previousSize)
      : base()
    {
      NewSize = newSize;
      PreviousSize = previousSize;
      RememberCommand = false;
    }

    public WindowResizeWozCommand(Size newSize)
      : base()
    {
      NewSize = newSize;

      // todo keep track that we need to save and load this ourself
      RememberCommand = true;
      RegisterObject();
    }

    public WindowResizeWozCommand()
      : base()
    {
    }

    public override void Redo()
    {
      if (this.WozClientCommandReferences != null)
      {


        if (this.WozClientCommandReferences.InkCanvas != null)
        {
          if (RememberCommand)
          {
            RegisterObject();
            PreviousSize = new Size(WozClientCommandReferences.InkCanvas.Width, WozClientCommandReferences.InkCanvas.Height);
          }
          this.WozClientCommandReferences.InkCanvas.Width = NewSize.Width;
          this.WozClientCommandReferences.InkCanvas.Height = NewSize.Height;
        }
        else if (this.WozClientCommandReferences.ClientWindow != null)
        {
          if (RememberCommand)
          {
            RegisterObject();
            PreviousSize = new Size(WozClientCommandReferences.ClientWindow.Width, WozClientCommandReferences.ClientWindow.Height);
          }
          this.WozClientCommandReferences.ClientWindow.Width = NewSize.Width;
          this.WozClientCommandReferences.ClientWindow.Height = NewSize.Height;
        }
      }
    }

    public override void Undo()
    {
      if (RememberCommand)
      {
        RememberSketchCommand wcrc = GetCommand(WozClientClearCommandID);
        if (wcrc != null && wcrc is WindowResizeWozCommand)
        {
          WindowResizeWozCommand wwrc = wcrc as WindowResizeWozCommand;
          if (wwrc.WozClientCommandReferences != null)
          {
            if (wwrc.WozClientCommandReferences.InkCanvas != null)
            {
              wwrc.WozClientCommandReferences.InkCanvas.Width = wwrc.PreviousSize.Width;
              wwrc.WozClientCommandReferences.InkCanvas.Height = wwrc.PreviousSize.Height;
            }
            if (wwrc.WozClientCommandReferences.ClientWindow != null)
            {
              wwrc.WozClientCommandReferences.ClientWindow.Width = wwrc.PreviousSize.Width;
              wwrc.WozClientCommandReferences.ClientWindow.Height = wwrc.PreviousSize.Height;
            }
          }
        }
      }
      else
      {
        if (this.WozClientCommandReferences != null)
        {
          if (this.WozClientCommandReferences.InkCanvas != null)
          {
            this.WozClientCommandReferences.InkCanvas.Width = PreviousSize.Width;
            this.WozClientCommandReferences.InkCanvas.Height = PreviousSize.Height;
          }
          else if (this.WozClientCommandReferences.ClientWindow != null)
          {
            this.WozClientCommandReferences.ClientWindow.Width = PreviousSize.Width;
            this.WozClientCommandReferences.ClientWindow.Height = PreviousSize.Height;
          }
        }
      }
    }
  }
}
